home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1996 #15
/
Monster Media Number 15 (Monster Media)(July 1996).ISO
/
os2
/
srefv112.zip
/
SREFPRC1.ZIP
/
DOCGI.SRF
< prev
next >
Wrap
Text File
|
1996-05-16
|
12KB
|
280 lines
/* This is don meyers cgi handler, modified for SRE-FILTER */
/* ----------------------------------------------------------------------- */
/* DoCGI: Handle branching of CGI scripts / subroutines. */
/* ----------------------------------------------------------------------- */
sref_DoCGI:
parse arg cgi_bin_dir, sel, verb, clientname0, filter_name, port , ,
servername, protocol, dir, who,tempfile,cmdfile
/*
1) cgi_bin_dir is of form d:\goserv\progs (dir where progs located --
strip final \). Should be set by configurator.
Note: cgi-bin\mapimage request strings are captured by SRE-FILTER, and not processed here
2) Sel is parsed, with scriptname, pathparms and list removed:
i.e.; if sel = cgi-bin/turkey/joe/proj1?arg1=wow
scriptname= turkey
This will run cgi_bin_dir\turkey (a cmd or exe file).
If you want to run programs NOT in the cgi_bin_dir directory,
pathparms= joe/proj
list= arg1=wow
3) Other variables are generic variables set in sre-filter
*/
parse var sel t1 '?' list
foo1=translate(sel,'/','\')
parse var t1 foocgi '/' scriptname '/' pathparms
if pathparms<>"" then pathparms=strip(pathparms,'t','/')
scriptname=strip(scriptname); cgi_bin_dir=strip(cgi_bin_dir)
say " CGI-Bin call for " scriptname
/* begin meyer stuff */
scriptalias=cgi_bin_dir
env='OS2ENVIRONMENT'
tempfile=translate(tempfile,'\','/')
if (verb == 'POST') then do
'read body var postedlist' /* get the incoming data */
if rc=-4 then /* body too large */
return response_dc('badreq', 'sent too much data')
if rc<>0 then /* e.g., invalid HTTP header */
return response_dc('badreq', 'sent data that could not be read')
end
ScriptName = translate(ScriptName)
/* we could check by extenstion (com, cmd, exe), but this might cause incorrect errors
So, we'll risk ugly errors below */
aa=sysfiletree(scriptalias'\'scriptname,'yow1','F')
if yow1.1 =0 then
return response_dc( 'notfound', 'cannot be honored. <p>This server does not currently support any CGI service called "'ScriptName'".')
/* else, do the script */
parse var ScriptAlias Drive':'Rest
if (Drive == ScriptAlias) then Drive = '' /* means no drive info to parse off... */
i = 1
_acc = REQFIELD("accept")
acc = '%'
ClientAccepts = ''
do while (acc \= _acc)
acc = REQFIELD("accept", i)
if (ClientAccepts \= '') then ClientAccepts = ClientAccepts','acc
else ClientAccepts = acc
i = i+1
end
rc = 0
name=0 then
name = ClientName()
rc = stream(tempfile, 'c', 'close') /* Close the file to avoid preventing process from access. */
/* This is pretty touchy stuff below, be very careful if you edit any of this... */
InputFile = translate( tempfile, '#', '$')
ReturnCode = '200' /* default return code */
call lineout CmdFile, "/**/"
call lineout CmdFile, "'@ECHO OFF'"
if (Drive \= '') then call lineout CmdFile, "'"Drive":'"
call lineout CmdFile, "'CD "ScriptAlias"'"
call lineout CmdFile, "env = '"env"'"
SrvVersionText = server('H')||' '||filter_name
rc = value('SERVER_SOFTWARE', SrvVersionText, env)
rc = value('GATEWAY_INTERFACE','CGI/1.1',env)
rc = value('SERVER_NAME',ServerName,env)
rc = value('SERVER_PORT',port,env)
i =1
l =1
ClientAccepts = ''
HeaderFile = translate( tempfile, '~', '$')
'READ HEADER FILE NAME 'HeaderFile /* get the incoming header data */
hd = linein( HeaderFile, 1)
do while (hd \= '')
hd = linein( HeaderFile)
parse var hd Hkey': 'content
Hkey = translate(Hkey, '_', '-')
Hkey = translate(Hkey)
select
when (Hkey == 'ACCEPT') then do
parse var content content'; 'q
if (i > 1) then ClientAccepts = ClientAccepts', 'content
else ClientAccepts = content
if (l == 5) then do
call lineout CmdFile, "'SET HTTP_ACCEPT=%HTTP_ACCEPT%"ClientAccepts"'"
ClientAccepts = ''
l = 1
end
l = l+1
i = i+1
end /* accept */
/* Handle other, unrecognized headers to conform to CGI/1.1 spec. */
otherwise do
if (Hkey \= '') then rc = lineout(CmdFile, "rc = value('HTTP_"Hkey"','"content"',env)")
end
end /*select */
end
rc = lineout( HeaderFile)
if (ClientAccepts \= '') then call lineout CmdFile, "'SET HTTP_ACCEPT=%HTTP_ACCEPT%"ClientAccepts"'"
crlf = '0d0a'x
output_text = "rc = value('SCRIPT_NAME','"ScriptName"',env)"crlf,
"rc = value('REQUEST_METHOD','"verb"',env)"crlf,
"rc = value('REMOTE_ADDR','"who"',env)"crlf,
"rc = value('SERVER_PROTOCOL','"protocol"',env)"crlf,
"rc = value('PATH_INFO','/"PathParms"',env)"crlf,
"rc = value('PATH_TRANSLATED','"dir||PathParms"',env)"crlf,
"rc = value('REMOTE_USER','"REQFIELD("from")"',env)"crlf,
"rc = value('AUTH_TYPE','"REQFIELD("auth-type")"',env)"crlf,
"rc = value('CONTENT_TYPE','"REQFIELD("Content-type")"',env)"crlf,
"rc = value('CONTENT_LENGTH','"REQFIELD("Content-length")"',env)"crlf,
"rc = value('REMOTE_HOST','"name"',env)"crlf,
"rc = value('QUERY_STRING','"list"',env)"
call lineout CmdFile, output_text
/* Change suggested by someone (lost the email) to allow 4OS2 to be used as shell. */
ScriptAlias = translate( ScriptAlias, '\', '/')
plist=packur(list) /* pack escape sequences in list */
if (plist \= '') then
if (pos('&', plist) > 0) | (pos('=', plist) > 0) | (pos("'", plist) > 0) then
/* plist = '"'plist'"' / * This line "quotes" the parameter list. Actual HTTPDs */
plist = '' /* simply omit the parameter list in this case. */
else do /* Process the parameter list back to original ascii format */
plist = translate( plist, ' ', '+')
end
if (verb == 'POST') then do
rc = charout( InputFile, postedlist, 1)
rc = stream( InputFile, 'C', 'close') /* Close file */
call lineout CmdFile, "'CALL "ScriptAlias"\"ScriptName" "plist" <"InputFile" >>"tempfile"'"
end
else do
call lineout CmdFile, "'CALL "ScriptAlias"\"ScriptName" "plist" >>"tempfile"'"
end
call lineout CmdFile /* Close file */
do
address cmd
CmdFile
rcode=RC
address
if (rcode == 0) then do
Hder = '%'
ContentType = 'text/html'
ContentLength = 0
do while (Hder \= '')
Hder = linein( tempfile)
if hder="" then iterate
parse var Hder Hkey': 'content
_Hkey = Hkey
_Hkey = translate( _Hkey)
/* This should handle the special header case of nph-* scripts... */
if (word(_Hkey,1) == 'HTTP/1.0') then do
parse var Hder Hkey content
_Hkey = 'STATUS'
'HEADER NOAUTO'
end
select
when (_Hkey == 'CONTENT-LENGTH') then ContentLength = content
when (_Hkey == 'CONTENT-TYPE') then ContentType = content
when (_Hkey == 'LOCATION') | (_Hkey == 'URI') then do
/* It is not 'spec' to assume a redirect if URI is included, but 'LOCATION' isn't really even 'spec'... */
if (_Hkey == 'LOCATION') then do
ReturnCode = '302'
'RESPONSE HTTP/1.0 'ReturnCode' Found' /* Set HTTP response line */
end
'HEADER ADD 'Hkey': 'content
end
when (_Hkey == 'STATUS') then do
parse var content ReturnCode rest
'RESPONSE HTTP/1.0 'content /* Set HTTP response line */
end
otherwise 'HEADER ADD 'Hkey': 'content /* oo */
end
end
_ContentLength = Chars(tempfile)
if ( _ContentLength < ContentLength) | (ContentLength == 0) then ContentLength = _ContentLength
'HEADER ADD Content-length: ' ContentLength
Content = charin( tempfile,, ContentLength)
Call Lineout tempfile /* Close file before delete */
end /* rcode=0 */
rc = SysFileDelete( tempfile) /* delete tempfile because we're shortening it. */
rc = SysFileDelete( CmdFile) /* delete CmdFile, we're done with it. */
if (verb == 'POST') then rc = SysFileDelete( InputFile) /* delete InputFile, we're done with it. */
rc = SysFileDelete( HeaderFile) /* delete HeaderFile, we're done with it. */
if (rcode \= 0) then return response_dc('badreq', 'could not be completed.<p><pre> Form Error: 'rcode'</pre>')
rc = charout( tempfile, Content, 1) /* Write contents back to tempfile */
rc = stream( tempfile, 'c', 'close')
'FILE ERASE TYPE 'ContentType' NAME' tempfile
return ''
end /* rcode=0 */
end
return response_dc('badreq', 'problem with CGI script "'scriptname'".')
/* ----------------------------------------------------------------------- */
/* RESPONSE_dc: Standard [mostly error] responses. */
/* ----------------------------------------------------------------------- */
/* This routine should stay in the main filter program. */
/* Arguments are: response type and extended message information. */
/* It returns the GoServe command to handle the result file. */
response_dc: procedure expose tempfile seloriginal request0 source0
parse arg request, message
select
when request='badreq' then use='400 Bad request syntax'
when request='notfound' then use='404 Not found'
when request='forbid' then use='403 Forbidden'
when request='unauth' then use='401 Unauthorized'
otherwise do
use='404 Not found'
say 'weird response ' request message
end
end /* Add others to this list as needed */
/* Now set the response and build the response file */
'RESPONSE HTTP/1.0' use /* Set HTTP response line */
parse var use code text
call lineout tempfile, '<!doctype html public "-//IETF//DTD HTML 2.0//EN">'
call lineout tempfile, "<html><head><title>"text"</title></head>"
call lineout tempfile, "<body><h2>Sorry...</h2>"
call lineout tempfile, "<p>The request from your Web client" message"."
call lineout tempfile, "<hr><em>HTTP response code:</em>" code '['text']'
call lineout tempfile, "<br><em>From server at:</em>" servername()
call lineout tempfile, "<br><em>Running:</em>" server()
call lineout tempfile, "</body></html>"
call lineout tempfile /* close */
return 'FILE ERASE TYPE text/html NAME' tempfile